package cn.bincker.mybatis.encrypt.converter.impl;

import cn.bincker.mybatis.encrypt.exception.InvalidDataException;

import java.nio.ByteBuffer;
import java.time.Month;

public class MonthEncryptConverter extends BaseEncryptConverter<Month>{
    @Override
    public Month convertNonNull(byte[] data) {
        if (data.length >= Integer.BYTES){
            return Month.of(ByteBuffer.wrap(data).getInt());
        }else if(data.length >= Short.BYTES){
            return Month.of(ByteBuffer.wrap(data).getShort());
        }
        throw new InvalidDataException("month need " + Integer.BYTES + " bytes but got " + data.length);
    }

    @Override
    public byte[] convertNonNull(Month object) {
        var result = new byte[Integer.BYTES];
        ByteBuffer.wrap(result).putInt(object.getValue());
        return result;
    }
}
